﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Management;
using System.Net.NetworkInformation;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace PowerMonitoring
{
    public partial class Service1 : ServiceBase
    {
        Thread _WorkThread = null;

        bool _Running = false;

        List<string> _TargetIP = new List<string>();

        int _CheckInterval = 1;
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            GetIPGateway(_TargetIP);
            _Running = true;
            _WorkThread = new Thread(WorkLoop);
            _WorkThread.Name = "Work Thread";
            _WorkThread.Start();
        }

        protected override void OnStop()
        {
            _Running = false;
            _WorkThread.Abort();
        }

        public void WorkLoop()
        {
            while(true)
            {
                if( !_Running )
                {
                    Thread.Sleep(1000);
                    continue;
                }

                Ping pingSender = new Ping();
                PingOptions options = new PingOptions();
                options.DontFragment = true;
                string data = "Sloong soft Network Test";
                byte[] buffer = Encoding.ASCII.GetBytes(data);
                int timeout = 120;
                bool bSucceed = false;
                foreach( var item in _TargetIP)
                {
                    PingReply reply = pingSender.Send(item, timeout, buffer, options);
                    if (reply.Status == IPStatus.Success)
                    {
                        bSucceed = true;
                        break;
                    }
                }
                if( !bSucceed )
                {
                    // Shutdown 
                    Shutdown();
                }
                else
                {
                    Thread.Sleep(_CheckInterval * 1000);
                }
            }
        }

        public void Shutdown()
        {
            Process myProcess = new Process(); 
            myProcess.StartInfo.FileName = "cmd.exe"; 
            myProcess.StartInfo.UseShellExecute = false; 
            myProcess.StartInfo.RedirectStandardInput = true; 
            myProcess.StartInfo.RedirectStandardOutput = true; 
            myProcess.StartInfo.RedirectStandardError = true; 
            myProcess.StartInfo.CreateNoWindow = true; 
            myProcess.Start(); 
            myProcess.StandardInput.WriteLine("shutdown -s -t 0");
        }

        public void GetIPGateway(List<string> list)
        {
            list.Clear();
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection nics = mc.GetInstances();
            foreach (ManagementObject nic in nics)
            {
                if (Convert.ToBoolean(nic["ipEnabled"]) == true)
                {
//                     MessageBox.Show((nic["IPAddress"] as String[])[0]);
//                     MessageBox.Show((nic["IPSubnet"] as String[])[0]);
                     list.Add((nic["DefaultIPGateway"] as String[])[0]);
                }
            }
        }
    }
}
